var housesSelect;// 项目下拉框
var buildingSelect;// 楼栋下拉框
var unitSelect;// 单元下拉框
var floorSelect;// 楼层下拉框
var functionSelect;//房间功能下拉选
var stateSelect;//房间状态
var feeStatisticalCategorySelect;//收款统计类别下拉选
var feeSubjectSelect;//收款科目
var arrearageSelect;//欠费类别
var baddebtsSelect;//坏账类别
var paymentMethodSelect;//收款方式
var paySubjectsSelect;//付款科目
var payableMethodSelect;//付款方式
var payableStatisticalSelect;//付款统计类别
var userSelect;//用户查询
var user2Select;//
var advanceFeeSubjectSelect;//预收科目
var offsetFeeSubjectSelect;//冲抵科目
var preferentialSelect;//优惠政策
var url;
var basePath;// 正确的url
var hidold;
var bidold;
var isShowDep;
var Psign;
$(document).ready(
		function() {
			url = window._server;
			basePath = url.replace("/ureport", "");
			$(".form-group>span").css({
				"line-height" : "34px",
				"padding-right" : "10px",
				"font-size" : "14px"
			});
			var v9=$(".form-horizontal .col-md-9").length;
			var v3=$(".form-horizontal .col-md-3").length;
			var obj3=$(".form-horizontal .col-md-3");
			var obj9=$(".form-horizontal .col-md-9");
			for(var i=0;i<v9;i++){
				$(obj9[i]).removeClass("col-md-9").addClass("col-md-8");
			}
			for(var i=0;i<v3;i++){
				$(obj3[i]).removeClass("col-md-3").addClass("col-md-4");
			}
			queryIsGroup();
			// 加载区域分公司树
			var setting = {
				check : {
					enable : true,
					chkStyle : "checkbox",
					chkboxType : {
						"Y" : "ps",
						"N" : "ps"
					}
				},
				callback : {
					onClick : function(e, treeId, treeNode, clickFlag) {
						var treeObj = $.fn.zTree.getZTreeObj(treeId);
						treeObj.checkNode(treeNode, !treeNode.checked, true);
					}
				},
				data : {
					simpleData : {
						enable : true
					}
				}
			};
			
			// 显示树
			$.ajax({
				url : basePath + '/zy/ureport/queryDepartmentByuserIdZtree.do',
				type : "get",
				cache : false,
				ifModified : true,
				scriptCharset : "utf-8",
				dataType : "json",
				success : function(result) {
					if (result == null) {
						return false;
					}
					$.fn.zTree.init($("#depztree"), setting, result.data);
				}
			});
			if ($("[name='depName']").length > 0) {
				$("[name='depName']").on("click", queryDepartment);
				var str="<input type='hidden' name='depId' id='depId'>";
				$($("[name='depName']")[0]).after(str);
			}
			/**
			 * 只有筛选条件包含 hid bid 时候加载一下方法
			 */
			if ($("[name='hid']").length > 0 ) {
				hidold = $("[name='hid']")[0].id;
				$("[name='hid']").attr("multiple", "multiple").addClass("multiple");
				housesSelect = $("[name='hid']").multiselect({
					beforeopen : function() {
						queryHouse("more",housesSelect);
						changeHouse();
					}
				});
				housesSelect.find('option').remove();
				housesSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			var spans=$(".form-group>span");
			if(spans.length>0){
				for(var t=0;t<spans.length;t++){
					var type=spans[t].innerHTML;
					switch (type) {
					 case "楼栋名称":
						 Psign="1";
						 queryExtend("zy_property_extend");
							break;
					 case "车场名称":
						 Psign="2";
						 alert(Psign);
							var str="<input type='hidden' name='extend' id='extend' value='"+extend+"'>";
							$("form").append(str);
						break;
					 case "广告名称":
						 Psign="3";
						 queryExtend("zy_adsense_extend");
							break;
					 case "场地名称":
						 Psign="4";
						 queryExtend("zy_venues_extend");
							break;
					 case "车场名称":
						 Psign="4";
						 queryExtend("zy_car_extend");
							break;
				
					}
				}
			}
	
			if ( $("[name='bid']").length > 0) {
				bidold = $("[name='bid']")[0].id;  
				$("[name='bid']").attr("multiple", "multiple").addClass(
						"multiple");
				buildingSelect = $("[name='bid']").multiselect({
					beforeopen : function() {
						queryBuilding(Psign);
					}
				});
				  buildingSelect.find('option').remove();
				  buildingSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 项目单选
			 */
			if($("[name='fee_hid']").length > 0){
				$("[name='fee_hid']").find('option').remove();
				$("[name='fee_hid']").change(function(){queryBuilding(Psign,"single");});
				changeHouse();
			}
			/**
			 * 房间功能
			 */
			if($("[name='function']").length > 0) {
				$("[name='function']").attr("multiple", "multiple").addClass(
						"multiple");
				functionSelect = $("[name='function']").multiselect({
					beforeopen : function() {
						queryFunction(Psign);
					}
				});
				functionSelect.find('option').remove();
				functionSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 房间状态
			 */
			if($("[name='state']").length > 0) {
				$("[name='state']").attr("multiple", "multiple").addClass(
						"multiple");
				stateSelect = $("[name='state']").multiselect({
					beforeopen : function() {
						queryState(Psign);
					}
				});
				stateSelect.find('option').remove();
				stateSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 收款统计类别
			 */
			if($("[name='feeStatisticalCategory']").length > 0) {
				$("[name='feeStatisticalCategory']").attr("multiple", "multiple").addClass(
						"multiple");
				feeStatisticalCategorySelect = $("[name='feeStatisticalCategory']").multiselect({
					beforeopen : function() {
						queryDictionary("statisticalCategory",feeStatisticalCategorySelect);
					}
				});
				feeStatisticalCategorySelect.find('option').remove();
				feeStatisticalCategorySelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 收费科目
			 */
			if($("[name='feeSubject']").length > 0) {
				$("[name='feeSubject']").attr("multiple", "multiple").addClass(
						"multiple");
				feeSubjectSelect = $("[name='feeSubject']").multiselect({
					beforeopen : function() {
						queryFeeSubject();
					}
				});
				feeSubjectSelect.find('option').remove();
				feeSubjectSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			 /**
			  * 欠费类别
			  */
			if($("[name='arrearage']").length > 0) {
				$("[name='arrearage']").attr("multiple", "multiple").addClass(
						"multiple");
				arrearageSelect = $("[name='arrearage']").multiselect({
					beforeopen : function() {
						queryArrearsBaddebt("1",arrearageSelect);
					}
				});
				arrearageSelect.find('option').remove();
				arrearageSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			 /**
			  * 坏账类别
			  */
			if($("[name='baddebts']").length > 0) {
				$("[name='baddebts']").attr("multiple", "multiple").addClass(
						"multiple");
				baddebtsSelect = $("[name='baddebts']").multiselect({
					beforeopen : function() {
						queryArrearsBaddebt("2",baddebtsSelect);
					}
				});
				baddebtsSelect.find('option').remove();
				baddebtsSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			 /**
			  * 收款方式
			  */
			if($("[name='paymentMethod']").length > 0) {
				$("[name='paymentMethod']").attr("multiple", "multiple").addClass(
						"multiple");
				paymentMethodSelect = $("[name='paymentMethod']").multiselect({
					beforeopen : function() {
						queryDictionary("paymentMethod",paymentMethodSelect);
					}
				});
				paymentMethodSelect.find('option').remove();
				paymentMethodSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 付款科目
			 */
			if($("[name='paySubjects']").length > 0) {
				$("[name='paySubjects']").attr("multiple", "multiple").addClass(
						"multiple");
				paySubjectsSelect = $("[name='paySubjects']").multiselect({
					beforeopen : function() {
						queryPaySubjects();
					}
				});
				paySubjectsSelect.find('option').remove();
				paySubjectsSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			 /**
			  * 付款方式
			  */
			if($("[name='payableMethod']").length > 0) {
				$("[name='payableMethod']").attr("multiple", "multiple").addClass(
						"multiple");
				payableMethodSelect = $("[name='payableMethod']").multiselect({
					beforeopen : function() {
						queryDictionary("payableMethod",payableMethodSelect);
					}
				});
				payableMethodSelect.find('option').remove();
				payableMethodSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 付款统计类别
			 */
			if($("[name='payableStatistical']").length > 0) {
				$("[name='payableStatistical']").attr("multiple", "multiple").addClass(
						"multiple");
				payableStatisticalSelect = $("[name='payableStatistical']").multiselect({
					beforeopen : function() {
						queryDictionary("payableStatistical",payableStatisticalSelect);
					}
				});
				payableStatisticalSelect.find('option').remove();
				payableStatisticalSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 用户查询1
			 */
			if($("[name='user']").length > 0) {
				$("[name='user']").attr("multiple", "multiple").addClass(
						"multiple");
				userSelect = $("[name='user']").multiselect({
					beforeopen : function() {
						queryUser(userSelect);
					}
				});
				userSelect.find('option').remove();
				userSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 用户查询2
			 */
			if($("[name='user2']").length > 0) {
				$("[name='user2']").attr("multiple", "multiple").addClass(
						"multiple");
				user2Select = $("[name='user2']").multiselect({
					beforeopen : function() {
						queryUser(user2Select);
					}
				});
				user2Select.find('option').remove();
				user2Select.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 表规格
			 */
			if($("[name='meterNorms']").length > 0) {
				$("[name='meterNorms']").attr("multiple", "multiple").addClass("multiple");
				$("[name='meterNorms']").find('option').remove();
				var option = '<option value="1">用户表</option><option value="2">公用表</option><option value="3">总表</option>';
				$("[name='meterNorms']").append(option);
				$("[name='meterNorms']").multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 表种类
			 */
			if($("[name='meterType']").length > 0) {
				$("[name='meterType']").attr("multiple", "multiple").addClass("multiple");
				$("[name='meterType']").find('option').remove();
				var option = '<option value="1">水表</option><option value="2">电表</option><option value="3">气表</option>';
				$("[name='meterType']").append(option);
				$("[name='meterType']").multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 预收科目
			 */
			if($("[name='advanceFeeSubject']").length > 0) {
				$("[name='advanceFeeSubject']").attr("multiple", "multiple").addClass(
						"multiple");
				advanceFeeSubjectSelect = $("[name='advanceFeeSubject']").multiselect({
					beforeopen : function() {
						queryAdvanceOffset("4",advanceFeeSubjectSelect);
					}
				});
				advanceFeeSubjectSelect.find('option').remove();
				advanceFeeSubjectSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 冲抵科目
			 */
			if($("[name='offsetFeeSubject']").length > 0) {
				$("[name='offsetFeeSubject']").attr("multiple", "multiple").addClass(
						"multiple");
				offsetFeeSubjectSelect = $("[name='offsetFeeSubject']").multiselect({
					beforeopen : function() {
						queryAdvanceOffset("3",offsetFeeSubjectSelect);
					}
				});
				offsetFeeSubjectSelect.find('option').remove();
				offsetFeeSubjectSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 优惠方式
			 */
			if($("[name='preferentialType']").length > 0) {
				$("[name='preferentialType']").attr("multiple", "multiple").addClass("multiple");
				$("[name='preferentialType']").find('option').remove();
				var option = '<option value="1">折扣</option><option value="2">任免</option><option value="3">减免</option><option value="4">赠送</option><option value="5">单价优惠</option>';
				$("[name='preferentialType']").append(option);
				$("[name='preferentialType']").multiselect().multiselectfilter().multiselect('refresh');
			}
			/**
			 * 优惠政策
			 */
			if($("[name='preferential']").length > 0) {
				$("[name='preferential']").attr("multiple", "multiple").addClass(
						"multiple");
				preferentialSelect = $("[name='preferential']").multiselect({
					beforeopen : function() {
						queryPreferential();
					}
				});
				preferentialSelect.find('option').remove();
				preferentialSelect.multiselect().multiselectfilter().multiselect('refresh');
			}
		});
// 查询是集团版还是普通版
function queryIsGroup() {
	$.ajax({
		type : 'post',
		url : basePath + "/zy/ureport/queryIsGroup.do",
		dataType : "json",
		async : false,
		success : function(result) {
			var data = result.data;
			if (data == "2") {// 不是集团版本 把区域分公司筛选去掉；
				$($("[name='depName']")[0].parentNode).hide();
				isShowDep = "2";
			} else {// 否则 显示他
				isShowDep="1";
				if($("[name='depName']").length > 0) {
					$($("[name='depName']")[0].parentNode).show();
				}
			}
		}
	});
}
// 查询当前人下的区域分公司
function queryDepartment() {
	$('#depModal .modal-title').text('请选择区域/分公司 ');
	$('#depModal').modal({
		keyboard : false,
		backdrop : 'static',
		show : true
	});
}
function adddep() {
	if ($("[name='hid']").length > 0 ) {
		housesSelect.find('option').remove();
		housesSelect.multiselect().multiselectfilter().multiselect('refresh');
	}
	$("#depModal").modal('hide');
    var arr=getCS();
    $("#depId").val(arr[0].substring(0, arr[0].length - 1));
    $("[name='depName']").val(arr[1].substring(0, arr[1].length - 1));
    if($("[name='depName']").val() != "" && $("[name='fee_hid']").length > 0){
    	queryHouse("single",$("[name='fee_hid']"));
    }else{
    	$("[name='fee_hid']").find('option').remove();
    }
}
// 根据权限查询项目名称
function queryHouse(type,select) {
	var depid="";
	if (select.val() == "") {
		select.find('option').remove();
		if(type == "more"){
			select.multiselect().multiselectfilter().multiselect('refresh');
		}
	}
	if($("[name='depName']").length > 0){
		  depid=$("#depId").val();
		  if(depid==""){
			  return;
		  }
		}
	if (select.find('option').length > 0) {
		return;
	}
	$.ajax({
		type : 'post',
		url : basePath + "/zy/ureport/queryHouse.do",
		data : {
			depid : depid
		},
		async : false,
		dataType : "json",
		success : function(result) {
			var data = result.data;
			if(type == "more"){
				select.multiselect().multiselectfilter().multiselect(
				'refresh');
				for (var i = 0; i < data.length; i++) {
					var opt = $('<option />', {
						value : data[i]["id"],
						text : data[i]["name"]
					});
					opt.appendTo(select);
				}
				select.multiselect('refresh');
			}else{
				var option = "<option value=''>请选择</option>";
				for(var i=0; i<data.length; i++){
					option += "<option value='"+ data[i]["id"] +"'>"+ data[i]["name"] +"</option>";
				}
				select.append(option);
			}
		}
	});
}
// 根据项目id或者权限查询楼栋
function queryBuilding(par,type) {
	var housesIds = "";
	if(type == "single"){
		housesIds = $("[name='fee_hid']").val();
		if(housesIds == "" || housesIds == null){
			buildingSelect.find('option').remove();
			buildingSelect.multiselect('refresh');
			return;
		}
	}else{
		$("input[name='multiselect_" + hidold + "']:checked").each(function() {
			housesIds = housesIds + $(this).val() + ",";
		});
		if (housesIds == "" || housesIds == null) {
			return;
		}
		if ($("input[name='multiselect_" + bidold + "']").length > 0) {
			return;
		}
		housesIds = housesIds.substring(0, housesIds.length - 1);
	}
	$.ajax({
		url : basePath + "/zy/ureport/queryBuildingByhid.do",
		type : 'post',
		cache : false,
		async : false,
		data : {
			hid : housesIds,
			sign:par
		},
		dataType : "json",
		success : function(result) {
			var data = result.data;
			buildingSelect.find('option').remove();
			buildingSelect.multiselect().multiselectfilter().multiselect('refresh');
			for (var i = 0; i < data.length; i++) {
				var opt = $('<option />', {
					value : data[i]["id"],
					text : data[i]["name"]
				});
				opt.appendTo(buildingSelect);
			}
			buildingSelect.multiselect('refresh');
		}
	});
}
/**
 * 拓展字段
 * @returns
 */
//function queryExtend(par){
//	$.ajax({
//		url : basePath + "/zy/ureport/queryExtend.do",
//		type : 'post',
//		async : false,
//		data:{
//			sign:par
//		},
//		dataType : "json",
//		success : function(result) {
//			var extend=result.data;
//		
//		}
//	});
	
}
// 点击项目时级联查询对应的楼栋
function changeHouse() {
	if ( $("[name='bid']").length > 0) {
		buildingSelect.find('option').remove();
		var multiple = buildingSelect.attr('multiple');
		if (!multiple) {
			buildingSelect.append("<option value=''>请选择</option>");
		}
		buildingSelect.multiselect('refresh');
	}

}

function dragWinClose(obj){
	var _obj = obj || this;
	$(_obj).parents('div.winDrag').modal('hide');
	
}
//获取选中节点
function getCS() {
	var treeObj = $.fn.zTree.getZTreeObj("depztree");
	var nodes = treeObj.getCheckedNodes(true);
	var all = new Array();
	var depid = "";
	var depname = "";
	var parm = 0;
	for (var i = 0; i < nodes.length; i++) {
		if (!nodes[i].getCheckStatus().half) {
			parm++;
			var type = nodes[i].sign;
			switch (type) {
			case "D":
				depid=depid+nodes[i].id+",";
				depname=depname+nodes[i].name+",";
				break;
			}
		}
	}
	all.push(depid);
	all.push(depname);
	return all;
}
//查询房间功能
function queryFunction(par){
	var sign = par;
	if (functionSelect.find('option').length > 0) {
		return;
	}
	$.ajax({
		url : basePath + "/zy/ureport/queryFunction.do",
		type : 'post',
		async : false,
		data:{
			sign:sign
		},
		dataType : "json",
		success : function(result) {
			var data = result.data;
			functionSelect.multiselect().multiselectfilter().multiselect('refresh');
			functionSelect.find('option').remove();
			for (var i = 0; i < data.length; i++) {
				var opt = $('<option />', {
					value : i+1,
					text : data[i]
				});
				opt.appendTo(functionSelect);
			}
			functionSelect.multiselect('refresh');
		}
	});
}
//查询房间状态
function queryState(par){
	var sign = par;
	if (stateSelect.find('option').length > 0) {
		return;
	}
	$.ajax({
		url : basePath + "/zy/ureport/queryState.do",
		type : 'post',
		async : false,
		data:{
			sign:sign
		},
		dataType : "json",
		success : function(result) {
			var data = result.data;
			stateSelect.multiselect().multiselectfilter().multiselect('refresh');
			stateSelect.find('option').remove();
			for (var i = 0; i < data.length; i++) {
				var opt = $('<option />', {
					value : i+1,
					text : data[i]
				});
				opt.appendTo(stateSelect);
			}
			stateSelect.multiselect('refresh');
		}
	});
}


}